//
// Created by djy on 2019/7/24.
//

#ifndef CLION_CODE_COMFUN_H
#define CLION_CODE_COMFUN_H

#ifndef _COMFUN_H
#define _COMFUN_H

#include <stdint.h>
#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif

#define BOOL int
#define TRUE 1
#define FALSE 0

typedef uint8_t  u8;
typedef uint16_t u16;
typedef uint32_t u32;

typedef int8_t   s8;


typedef long            base_t;      /* Nbit CPU related date type */
typedef unsigned long   ubase_t;     /* Nbit unsigned CPU related data type */


#define log_d(...) printf(__VA_ARGS__)
#define log_arr_d(array, len)   {									\
                                int i;							\
                                for(i=0;i<len;i++)				\
                                {								\
                                    log_d("%02X ",array[i]);	\
                                }								\
                                log_d("\n");					\
                              }


#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))

#define set_bit(x, bit) ((x) |= 1 << (bit))
#define clr_bit(x, bit) ((x) &= ~(1 << (bit)))
#define tst_bit(x, bit) ((x) & (1 << (bit)))
#define get_bits(val,x1,x2) (((val)>>(x1))&((1<<((x2)-(x1)+1))-1))

#ifndef min
#define min(a, b) ((a)<(b) ? (a):(b))
#endif
#ifndef max
#define max(a, b) ((a)>(b) ? (a):(b))
#endif

#define bcd2bin(val) (((val) & 0x0f) + ((val) >> 4) * 10)
#define bin2bcd(val) ((((val) / 10) << 4) + (val) % 10)
#define abs(a) ((a) < 0 ? -(a) : (a))
#define swap(a, b) do {a ^= b; b ^= a; a ^= b;} while (0)
# define offset_of(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)

u8 checksum(const void *data, u16 len);
void os_memcpy(void *dst, const void *src, u16 len) ;
u8 os_memcmp(const void *s1, const void *s2, u16 n);
void os_memset(void *s1, u8 val, u16 len);
void os_memset(void *s1, u8 val, u16 len);
u8 is_all_xx(const void *s1, u8 val, u16 n);
int is_data_all_xx(const unsigned char data[], int len, unsigned char x);
u32 get_le_val(const u8 * p, u8 bytes);
void put_le_val(u32 val, u8 * p, u8 bytes);
void * os_memmove(void *dest, const void *src, u16 n);
unsigned int get_int_para(unsigned char buff[],int i);
unsigned short add_crc16(unsigned char *buffer, unsigned char data_length);

unsigned int get_int_para(unsigned char buff[],int i);
unsigned short get_short_para(unsigned char buf[],int i);
int  hexarry2str(char str[],int str_len,unsigned char var[],int var_size);
const char *arr2str(const void *arr, int len, void *dest, int maxlen);
uint8_t get_one_nums(uint8_t byte);
void hex2bcd(uint32_t value, uint8_t *bcd, uint8_t bytes);
uint32_t bcd2hex(uint8_t *bcd, uint8_t bytes);
void float_to_bcd(float f, ubase_t bytes, ubase_t frabits, u8 *bcd);
float bcd_to_float(u8 *bcd, ubase_t bytes, ubase_t frabits, BOOL withsign);
#ifdef __cplusplus
}
#endif

#endif






#endif //CLION_CODE_COMFUN_H
